package org.mcb.zebra.portal.url;

import javax.portlet.PortletMode;
import javax.portlet.WindowState;
import javax.servlet.http.HttpServletRequest;

import org.apache.pluto.PortletWindow;
import org.mcb.zebra.portal.manager.PortalManager;
import org.mcb.zebra.portal.manager.ZebPortalManager;
import org.mcb.zebra.portal.page.Page;
import org.mcb.zebra.portal.page.PageManager;
import org.mcb.zebra.portal.url.PortalUrl.PortletRequestMode;

public class ZebUrlParser implements UrlParser {
	public final String PORTAL_INFOR = "PORTAL_INFOR";
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mcb.zebra.portal.url.UrlParser#getPageName(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public String getPageName(HttpServletRequest request) {
		return (String) request.getAttribute(PageManager.CURRENT_PAGE);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mcb.zebra.portal.url.UrlParser#getPortletWindow(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public PortletWindow getPortletWindow(
			HttpServletRequest httpServletRequest, String portletId) {
		String pageName = getPageName(httpServletRequest);
		PortalManager manager = ZebPortalManager.getInstance();
		PageManager pageManager = manager.getPageManager();
		if (pageManager != null) {
			Page page = pageManager.getPage(pageName);
			return page == null ? null : portletId == null ? null : page
					.getPortletWindow(portletId);
		}
		return null;
	}

	@Override
	public PortalUrl getPortalUrl(HttpServletRequest httpServletRequest) {
		PortalUrl portalUrl = null;
		getPortalInfo(httpServletRequest);
		String pathInfor = httpServletRequest.getPathInfo();
		if (pathInfor != null) {
			pathInfor =  pathInfor.substring(1);
			int index = pathInfor.indexOf("~");
			if (index > 0) {
				String portletWindowId = pathInfor.substring(0, index);
				String page = getPageName(httpServletRequest);
				if (page != null) {
					PortalManager manager = ZebPortalManager.getInstance();
					Page page2 = manager.getPageManager().getPage(page);
					if (page2 != null
							&& page2.containsPortletWindow(portletWindowId)) {
						portalUrl = new ZebPortalUrl();
						portalUrl.setAction(PortletRequestMode.NONE);
						portalUrl.setPorletWindowID(portletWindowId);
						String modeOrStatus;
						if (pathInfor.indexOf('~', index + 1) > 0) {
							portalUrl.setAction(PortletRequestMode.ACTION);
							modeOrStatus = pathInfor.substring(index + 1,
									pathInfor.indexOf('~', index + 1) );
						} else{
							modeOrStatus = pathInfor.substring(index + 1);
							portalUrl.setAction(PortletRequestMode.RENDER);
						}
						if (modeOrStatus != null) {
							String[] temp = modeOrStatus.split("[!]");
							if (temp != null && temp.length == 2) {
								portalUrl.setMode(new PortletMode(temp[0]));
								portalUrl.setState(new WindowState(temp[1]));
							}
						}
					}
				}

			}
		}
		return portalUrl;
	}

	@Override
	public String getPortalInfo(HttpServletRequest request) {
		String pathInfo = (String) request.getAttribute(PORTAL_INFOR);
		if(pathInfo == null){
			pathInfo =  request.getServletPath();
			request.setAttribute(PORTAL_INFOR, pathInfo);
		}
		return pathInfo;
	}

	@Override
	public String getPortalServlet(HttpServletRequest request) {
		
		return request.getContextPath();
	}
}
